package memoryPatch;

//import java.io.BufferedReader;
//import java.io.IOException;
import java.util.Date;
import java.util.Random;
import java.util.logging.Logger;
import org.json.JSONException;
import org.json.JSONObject;

import com.google.appengine.api.datastore.Entity;


public class Task {

	private static final Logger log = Logger.getLogger(Task.class.getName());
	
	public Task() {}
	
	public Task(String request, String user) {
		JSONObject jsonObject = null;
		try {
		    jsonObject = new JSONObject(request);
		} catch (JSONException e) {
		    log.warning("Error parsing JSON from request string");
		}
		this.user = user;
		this.creationDate = new Date();
		if (jsonObject.has("taskId") == false) {
			Random rand = new Random();
			Date now = new Date();
			rand.setSeed(now.getTime());
			this.taskId = rand.nextLong();
		}
		else {
			try {
				this.taskId = jsonObject.getLong("taskId");
			} catch (JSONException e) {
				log.severe("Couldn't get taskId from jsonObject");
				log.severe("JSONObject:" + jsonObject.toString());
				e.printStackTrace();
				this.taskId = new Date().getTime();
			}
		}
		// parameters passed in are currently: content, done, and order
		if (jsonObject != null) {
			try {
				this.content = jsonObject.getString("content");
				this.done = (Boolean) jsonObject.get("done");
				this.order = (Integer) jsonObject.get("order");
			} catch (Exception e) {
				log.warning("Error parsing JSON parameters");
			}
		}
		String output = "Parsed task object: '" + this.content + "' " +
				this.done.toString() + " " + this.order +  " '" + this.user + "' " + this.creationDate;
		log.info(output);
	}
	
	public Task(Entity taskEnt) {
		this.user = (String) taskEnt.getProperty("user");
		this.content = (String) taskEnt.getProperty("content");
		this.done = (Boolean) taskEnt.getProperty("done");
		this.order = (Long) taskEnt.getProperty("order");
		this.creationDate = (Date) taskEnt.getProperty("creationDate");
		if (taskEnt.hasProperty("taskId")) { 
			this.taskId = (Long) taskEnt.getProperty("taskId");
		}
		else {
			this.taskId = new Date().getTime();
		}
			
	}
	
	
	
	public String getJSONString() {
		String jsonString = "";
		JSONObject jsonObject = new JSONObject();
		try {
			jsonObject.put("user", this.user);
			jsonObject.put("content", this.content);
			jsonObject.put("done", this.done);
			jsonObject.put("order", this.order);
			jsonObject.put("creationDate", this.creationDate);
			jsonObject.put("taskId", this.taskId);
		} catch (JSONException e) {
			log.warning("Could not make JSONObject from task");
		}
		jsonString = jsonObject.toString();
		return jsonString;
	}
	
	public JSONObject getJSONObject() {
		JSONObject jsonObject = new JSONObject();
		try {
			jsonObject.put("user", this.user);
			jsonObject.put("content", this.content);
			jsonObject.put("done", this.done);
			jsonObject.put("order", this.order);
			jsonObject.put("creationDate", this.creationDate);
			jsonObject.put("taskId", this.taskId);
		} catch (JSONException e) {
			log.warning("Could not make JSONObject from task");
		}
		return jsonObject;
	}
	
	public Entity makeEntity() {
		Entity taskEnt = new Entity("Task");
		taskEnt.setProperty("content", this.content);
		taskEnt.setProperty("done", this.done);
		taskEnt.setProperty("order", this.order);
		taskEnt.setProperty("user", this.user);
		taskEnt.setProperty("creationDate", this.creationDate);
		taskEnt.setProperty("taskId", this.taskId);
		return taskEnt;
	}
	
	public long getTaskId() {
		return taskId;
	}
	
	public void setEntity(Entity taskEnt) {
		taskEnt.setProperty("content", this.content);
		taskEnt.setProperty("done", this.done);
		taskEnt.setProperty("order", this.order);
		taskEnt.setProperty("user", this.user);
		taskEnt.setProperty("creationDate", this.creationDate);
		taskEnt.setProperty("taskId", this.taskId);
	}
	//private long taskid;
	private String content;
	private String user;
	private Boolean done;
	//private Boolean deleted;
	private long taskId;
	private long order;
	private Date creationDate;
	//private String[] readers;
	//private String[] writers;
	//private String[] hashtags;
}
